package com.hc.core.util.pageStatic;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import com.hc.core.util.DateTool;
import com.hc.core.util.FileTool;

public class IndexToHtml extends HttpServlet {
	String requestUrl = "/index.htm";// 请求的URL,响应内容静态化

	String createIndexHtmlFileName = "index_new.html";
	String visitIndexHtmlFileName = "index.html";

	public void service(HttpServletRequest request, HttpServletResponse response) {
		String createIndexHtmlFilePath = request.getRealPath("/"
				+ createIndexHtmlFileName);
		String visitIndexHtmlFilePath = request.getRealPath("/"
				+ visitIndexHtmlFileName);

		// 第一步:把服务器响应内容输出到静态页面
		long startTime = new Date().getTime();
		long createTime = 0;

		// ConfConstants.CONTEXT_PATH为你的应用的上下文路径。
		ServletContext sc = getServletContext();
		RequestDispatcher requestDispartcher = sc
				.getRequestDispatcher(requestUrl);

		final ByteArrayOutputStream os = new ByteArrayOutputStream();
		final ServletOutputStream stream = new ServletOutputStream() {
			public void write(byte[] data, int offset, int length) {
				os.write(data, offset, length);
			}

			public void write(int b) throws IOException {
				os.write(b);
			}
		};

		final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
		HttpServletResponse httpServletResponse = new HttpServletResponseWrapper(
				response) {
			public ServletOutputStream getOutputStream() {
				return stream;
			}

			public PrintWriter getWriter() {
				return pw;
			}
		};

		FileOutputStream fos = null;
		try {
			requestDispartcher.include(request, httpServletResponse);
			pw.flush();

			fos = new FileOutputStream(createIndexHtmlFilePath); // 把服务器响应的内容输出到静态页面
			os.writeTo(fos);

			createTime = new Date().getTime() - startTime;
		} catch (IOException e) {
			System.out.print("首页静态页生成失败!IO异常:" + e.getMessage());
		} catch (ServletException e) {
			System.out.print("首页静态页生成失败!Servlet异常:" + e.getMessage());
		} finally {
			try {
				fos.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		// 第二步:newHtmlFilePath拷贝到htmlFilePath
		try {
			long copyTime = FileTool.forChannel(createIndexHtmlFilePath,
					visitIndexHtmlFilePath);
			
			response.setContentType("text/html;charset=GBK"); 
			PrintWriter out = response.getWriter();
			
			
			StringBuffer message = new StringBuffer();
			message.append(DateTool.getDateTime("yyyy-MM-dd HH:mm:ss"));
			message.append("首页静态页生成成功!耗时【");
			message.append(createTime);
			message.append("毫秒 + ");
			message.append(copyTime);
			message.append("毫秒】");

			out.print(message.toString());
			System.out.println(message.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 第三步:删除newHtmlFilePath
		File file = new File(createIndexHtmlFilePath);
		if (file.exists()) {
			file.delete();
		}
	}
}